package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.*;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/students")
@Slf4j
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;


    //学员列表查询
    //请求路径：/students
    //请求方式：GET
    //接口描述：该接口用于学员列表数据的条件分页查询
    @GetMapping
    public Result page(StudentQueryParam empQueryParam) {

        log.info("学员列表查询");
        PageResult<Student> pageResult = studentService.page(empQueryParam);
        return Result.success(pageResult);

    }

    //删除学员
    //请求路径：/students/{ids}
    //请求方式：DELETE
    //接口描述：该接口用于批量删除学员信息
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids){
        log.info("删除学员信息，ids:{}",ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }


    //添加学员
    //基本信息
    //请求路径：/students
    //请求方式：POST
    //接口描述：该接口用于添加学员信息
    @Log
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("添加学员信息,{}", student);
        studentService.save(student);
        return Result.success();

    }
    //    修改学员
    //1.根据ID查询 查询回显
    @Log

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        log.info("根据ID查询学员信息，id:{}",id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    //2.保存修改的学员信息 更新
    @Log
    @PutMapping
    public Result update(@RequestBody Student student){

        log.info("修改学员信息,{}", student);
        studentService.update(student);
        return Result.success();

    }

    //违纪处理
    //请求路径：/students/violation/{id}/{score}
    //请求方式：PUT
    //接口描述：该接口用于修改学员的数据信息
    //违纪处理一次，需要将违纪次数+1，违纪扣分+前端输入的分数
    @Log
    @PutMapping("/violation/{id}/{score}")
    //PathVariable注解用于获取请求路径中的参数
    public Result violation(@PathVariable("id") Integer id,@PathVariable("score") Integer score) {
        log.info("违纪处理，id:{},score:{}",id,score);
        studentService.violation(id,score);
        return Result.success();
    }


}
